﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;

namespace ExpertMIS.UserControl
{
    public partial class ucColumnList2 : System.Web.UI.UserControl
    {
        private string[] _expertColumnsOID = null;
        private int _articleCountOnColumn = 6;
        private Dictionary<string, string> _dictColumnClass = new Dictionary<string, string>();
        private ADMColumnEntity _currentColumn = null;

        private void GetColumnArticle(string ColumnOID, Repeater repColumn)
        {
            ADMArticleBroker broker = new ADMArticleBroker();

            repColumn.DataSource = broker.GetArticlesByColumn(ColumnOID, _articleCountOnColumn);
            repColumn.DataBind();
        }

        private void RefreshColumns()
        {
            ADMColumnBroker broker = new ADMColumnBroker();
            IList<ADMColumnEntity> list = broker.GetRootColumns(4);

            foreach (ADMColumnEntity column in list)
            {
                bool bRemove = false;
                foreach (string oid in _expertColumnsOID)
                {
                    if (oid == column.OID)
                    {
                        bRemove = true;
                        break;
                    }
                }

                if (bRemove)
                {
                    list.Remove(column);
                }
            }

            repColumnList.DataSource = list;
            repColumnList.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _dictColumnClass.Add("102", "");
            _dictColumnClass.Add("101", "tain_1");
            _dictColumnClass.Add("103", "tain_2");
            _dictColumnClass.Add("104", "tain_3");

            _expertColumnsOID = ExpertMIS.Codes.Utility.GetEnvironmentValue("leftcolumns").Split(',');
            RefreshColumns();
        }

        protected void repColumnList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater repColumn = e.Item.FindControl("repColumn") as Repeater;
                _currentColumn = e.Item.DataItem as ADMColumnEntity;
                if (repColumn != null && _currentColumn != null)
                {
                    repColumn.ItemDataBound += new RepeaterItemEventHandler(repColumn_ItemDataBound);
                    GetColumnArticle(_currentColumn.OID, repColumn);
                }
            }
        }

        void repColumn_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Header && _currentColumn != null)
            {
                System.Web.UI.HtmlControls.HtmlGenericControl h1Header = e.Item.FindControl("h1Header") as System.Web.UI.HtmlControls.HtmlGenericControl;
                if (h1Header != null && _dictColumnClass.ContainsKey(_currentColumn.OID))
                {
                    h1Header.Attributes["class"] = _dictColumnClass[_currentColumn.OID];
                }
                System.Web.UI.HtmlControls.HtmlAnchor anchorTitle = e.Item.FindControl("anchorTitle") as System.Web.UI.HtmlControls.HtmlAnchor;
                if (anchorTitle != null && _currentColumn != null)
                {
                    anchorTitle.InnerText = _currentColumn.Name;
                    anchorTitle.HRef = ResolveUrl("~/Columns/ColumnView.aspx?OID=") + _currentColumn.OID;
                }

                System.Web.UI.HtmlControls.HtmlAnchor anchorMore = e.Item.FindControl("anchorMore") as System.Web.UI.HtmlControls.HtmlAnchor;
                if (anchorMore != null && _currentColumn != null)
                {
                    anchorMore.HRef = ResolveUrl("~/Columns/ColumnView.aspx?OID=") + _currentColumn.OID;
                }
            }
        }
    }
}